//
// Created by sally on 24-10-28.
//

#include "Task_Result.h"
#include <iostream>
using namespace whq::timer;

Task_Result::Task_Result(std::shared_ptr<TimeTask> ptr)
    :is_loop(ptr->isLoop())
{
    if (is_loop)
        w_tmTask = ptr;
    else
        s_tmTask = ptr;
}

Task_Result::~Task_Result()
{
}

bool Task_Result::get_result(const uint32_t milliseconds)
{
    auto span= std::chrono::microseconds(milliseconds);

    std::shared_ptr<TimeTask> sp;
    if (is_loop)
        sp = w_tmTask.lock();
    else
        sp = s_tmTask;

    if (sp)
    {
        future<bool> & result = sp->get_execute_result();
        if (result.valid())
        {
            try
            {
                //获取任务的结果,wait_for在指定时间内等待任务完成，返回状态：timeout、ready 或 deferred。
                if (result.wait_for(span) != future_status::timeout)
                {
                    return result.get();
                }
            }
            catch (std::exception&)
            {
                return false;
            }

        }
    }

    return false;
}
